
// Definition for singly-linked list.
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
    // 找中点后分离链表(向上取整)
    ListNode *detach_list(ListNode *head)
    {
        ListNode *slow = head;
        ListNode *fast = head;
        while (fast && fast->next && fast->next->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }
        ListNode *ret = slow->next;
        slow->next = nullptr;
        return ret;
    }
    // 反转链表
    ListNode *reverse_list(ListNode *head)
    {
        ListNode *pev = nullptr;
        ListNode *cur = head;
        while (cur)
        {
            ListNode *next = cur->next;
            cur->next = pev;
            pev = cur;
            cur = next;
        }
        return pev;
    }
    // 一一比对
    bool isPalindrome(ListNode *head)
    {
        ListNode *l1 = head;
        ListNode *l2 = reverse_list(detach_list(head));
        while (l1 && l2)
        {
            if (l1->val != l2->val)
                return false;
            l1 = l1->next;
            l2 = l2->next;
        }
        return true;
    }
};